package com.mtons.mblog.modules.service;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.modules.data.AnswerVO;
import com.mtons.mblog.modules.entity.Answer;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Logan
 */
@CacheConfig(cacheNames = Consts.CACHE_USER)
public interface AnswerService {
    /**
     * 分页查询所有回答
     *
     * @param pageable
     * @param questId 问题Id
     */
    @Cacheable
    Page<AnswerVO> paging(Pageable pageable, long questId, Set<Long> excludeQuestIds);

    Page<AnswerVO> paging4Admin(Pageable pageable, long questId, String title);

    Page<AnswerVO> paging4Admin(Pageable pageable, String content);

    /**
     * 查询个人发布回答
     * @param pageable
     * @param userId
     */
    @Cacheable
    Page<AnswerVO> pagingByAuthorId(Pageable pageable, long userId);

    /**
     * 查询最近更新 - 按发布时间排序
     * @param maxResults
     * @return
     */
    @Cacheable(key = "'latest_' + #maxResults")
    List<AnswerVO> findLatestAnswer(int maxResults);

    /**
     * 查询热门回答 - 按浏览次数排序
     * @param maxResults
     * @return
     */
    @Cacheable(key = "'hottest_' + #maxResults")
    List<AnswerVO> findHottestAnswer(int maxResults);

    List<AnswerVO> findHottestAnswerByQustId(long questId, int maxResults, long excludeAnswerId);

    /**
     * 根据Ids查询
     * @param ids
     * @return <id, 回答对象>
     */
    Map<Long, AnswerVO> findMapByIds(Set<Long> ids);

    /**
     * 发布回答
     * @param answer
     */
    @CacheEvict(allEntries = true)
    long post(AnswerVO answer);

    /**
     * 回答详情
     * @param id
     * @return
     */
    @Cacheable(key = "'answer_' + #id")
    AnswerVO get(long id);

    /**
     * 更新回答方法
     * @param a
     */
    @CacheEvict(allEntries = true)
    void update(AnswerVO a);

    /**
     * 推荐/精华
     * @param id
     * @param featured 0: 取消, 1: 加精
     */
    @CacheEvict(allEntries = true)
    void updateFeatured(long id, int featured);

    /**
     * 置顶
     * @param id
     * @param weighted 0: 取消, 1: 置顶
     */
    @CacheEvict(allEntries = true)
    void updateWeight(long id, int weighted);

    /**
     * 带作者验证的删除 - 验证是否属于自己的回答
     * @param id
     * @param authorId
     */
    @CacheEvict(allEntries = true)
    void delete(long id, long authorId);

    @CacheEvict(allEntries = true)
    void deleteByQuest(long questId);

    /**
     * 批量删除回答, 且刷新缓存
     *
     * @param ids
     */
    @CacheEvict(allEntries = true)
    void delete(Collection<Long> ids);

    /**
     * 自增浏览数
     * @param id
     */
    @CacheEvict(key = "'view_' + #id")
    void identityViews(long id);

    /**
     * 自增评论数
     * @param id
     */
    @CacheEvict(key = "'view_' + #id")
    void identityComments(long id);

    /**
     * 喜欢回答
     * @param userId
     * @param answerId
     */
    @CacheEvict(key = "'view_' + #answerId")
    void favor(long userId, long answerId);

    /**
     * 取消喜欢回答
     * @param userId
     * @param answerId
     */
    @CacheEvict(key = "'view_' + #answerId")
    void unfavor(long userId, long answerId);

    long count();
}
